package cn.iocoder.an.module.business.controller.admin.auditplandetail;

import cn.iocoder.an.module.business.controller.admin.auditplanarea.vo.AuditPlanAreaRespVO;
import cn.iocoder.an.module.business.dal.dataobject.auditplanarea.AuditPlanAreaDO;
import cn.iocoder.an.module.business.dal.mysql.auditplanarea.AuditPlanAreaMapper;
import com.fhs.core.trans.anno.TransMethodResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import jakarta.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import jakarta.validation.constraints.*;
import jakarta.validation.*;
import jakarta.servlet.http.*;
import java.util.*;
import java.io.IOException;

import cn.iocoder.an.framework.common.pojo.PageParam;
import cn.iocoder.an.framework.common.pojo.PageResult;
import cn.iocoder.an.framework.common.pojo.CommonResult;
import cn.iocoder.an.framework.common.util.object.BeanUtils;
import static cn.iocoder.an.framework.common.pojo.CommonResult.success;

import cn.iocoder.an.framework.excel.core.util.ExcelUtils;

import cn.iocoder.an.framework.apilog.core.annotation.ApiAccessLog;
import static cn.iocoder.an.framework.apilog.core.enums.OperateTypeEnum.*;

import cn.iocoder.an.module.business.controller.admin.auditplandetail.vo.*;
import cn.iocoder.an.module.business.dal.dataobject.auditplandetail.AuditPlanDetailDO;
import cn.iocoder.an.module.business.service.auditplandetail.AuditPlanDetailService;

@Tag(name = "管理后台 - 审核填报")
@RestController
@RequestMapping("/business/audit-plan-detail")
@Validated
public class AuditPlanDetailController {



    @Resource
    private AuditPlanDetailService auditPlanDetailService;
    @Autowired
    private AuditPlanAreaMapper auditPlanAreaMapper;

    @PostMapping("/create")
    @Operation(summary = "创建审核填报")
    public CommonResult<Long> createAuditPlanDetail(@Valid @RequestBody AuditPlanDetailSaveReqVO createReqVO) {
        return success(auditPlanDetailService.createAuditPlanDetail(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新审核填报")
    public CommonResult<Boolean> updateAuditPlanDetail(@Valid @RequestBody AuditPlanDetailSaveReqVO updateReqVO) {
        auditPlanDetailService.updateAuditPlanDetail(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除审核填报")
    @Parameter(name = "id", description = "编号", required = true)
    public CommonResult<Boolean> deleteAuditPlanDetail(@RequestParam("id") Long id) {
        auditPlanDetailService.deleteAuditPlanDetail(id);
        return success(true);
    }

    @DeleteMapping("/delete-list")
    @Parameter(name = "ids", description = "编号", required = true)
    @Operation(summary = "批量删除审核填报")
    public CommonResult<Boolean> deleteAuditPlanDetailList(@RequestParam("ids") List<Long> ids) {
        auditPlanDetailService.deleteAuditPlanDetailListByIds(ids);
        return success(true);
    }

    @TransMethodResult
    @GetMapping("/get")
    @Operation(summary = "获得审核填报")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    public CommonResult<AuditPlanDetailRespVO> getAuditPlanDetail(@RequestParam("id") Long id) {
        AuditPlanDetailDO auditPlanDetail = auditPlanDetailService.getAuditPlanDetail(id);
        AuditPlanDetailRespVO bean = BeanUtils.toBean(auditPlanDetail, AuditPlanDetailRespVO.class);
        Long planAreaId = bean.getPlanAreaId();
        AuditPlanAreaDO auditPlanAreaDO = auditPlanAreaMapper.selectById(planAreaId);
        AuditPlanAreaRespVO planAreaRespVO = BeanUtils.toBean(auditPlanAreaDO, AuditPlanAreaRespVO.class);
        bean.setPlanAreaRespVO(planAreaRespVO);
        return success(bean);
    }

    @TransMethodResult
    @GetMapping("/page")
    @Operation(summary = "获得审核填报分页")
    public CommonResult<PageResult<AuditPlanDetailRespVO>> getAuditPlanDetailPage(@Valid AuditPlanDetailPageReqVO pageReqVO) {
        pageReqVO.setAuditResult("Y");
        PageResult<AuditPlanDetailDO> pageResult = auditPlanDetailService.getAuditPlanDetailPage(pageReqVO);
        PageResult<AuditPlanDetailRespVO> detailBean = BeanUtils.toBean(pageResult, AuditPlanDetailRespVO.class);
        List<AuditPlanDetailRespVO> detailBeanList = detailBean.getList();
        for (AuditPlanDetailRespVO detail : detailBeanList) {
            Long planAreaId = detail.getPlanAreaId();
            AuditPlanAreaDO auditPlanAreaDO = auditPlanAreaMapper.selectById(planAreaId);
            AuditPlanAreaRespVO planAreaRespVO = BeanUtils.toBean(auditPlanAreaDO, AuditPlanAreaRespVO.class);
            detail.setPlanAreaRespVO(planAreaRespVO);
        }
        return success(detailBean);
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出审核填报 Excel")
    @ApiAccessLog(operateType = EXPORT)
    public void exportAuditPlanDetailExcel(@Valid AuditPlanDetailPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<AuditPlanDetailDO> list = auditPlanDetailService.getAuditPlanDetailPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "审核填报.xls", "数据", AuditPlanDetailRespVO.class,
                        BeanUtils.toBean(list, AuditPlanDetailRespVO.class));
    }

}